<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\member\controller;

/** 控制器类引入区 **/

/** 模型类引入区 **/
use app\common\model\Order as OrderM;
use app\common\model\ProductOrder as ProductOrderM;

/** 核心或第三方类引入区 **/
use think\Db;


/**
 * 支付回调 控制器
 * @package app\api\controller
 */
class Notify extends Base
{
    public $noNeedLogin = ['*'];

    /**
     *   门店项目 回调
     **/
    public function wx_pj_notify_url(){
        // 获取xml
        $xml        =   file_get_contents('php://input', 'r');
        // 转成php数组
        $arr        =   $this->toArray($xml);
        //商户订单号
        $order_sn   =   $arr['out_trade_no'];
        //支付宝交易号
        $pay_sn      =   $arr['transaction_id'];
        //订单金额
        $money      =   $arr['total_fee'];
        $order      =   OrderM::where(['pay_no'=>$order_sn])->find();
        if($order['pay_status'] == 1)
        {
            echo "success";
            return;
        }
        if($order){

            Db::startTrans();
            try{

                $orderEdit['status'] = 1;
                $orderEdit['pay_status'] = 2;
                $orderEdit['pay_type'] = 1;

                OrderM::where('id',$order)->update($orderEdit);


                Db::commit();
                echo "success";
            }catch (\Exception $e){

                Db::rollback();
                echo "fail";
            }

        }else{
            echo "fail";
        }
    }

    /**
     *   门店商品 回调
     **/
    public function wx_pd_notify_url(){
        // 获取xml
        $xml        =   file_get_contents('php://input', 'r');
        // 转成php数组
        $arr        =   $this->toArray($xml);
        //商户订单号
        $order_sn   =   $arr['out_trade_no'];
        //支付宝交易号
        $pay_sn      =   $arr['transaction_id'];
        //订单金额
        $money      =   $arr['total_fee'];
        $order      =   ProductOrderM::where(['pay_no'=>$order_sn])->find();
        if($order['pay_status'] == 1)
        {
            echo "success";
            return;
        }
        if($order){

            Db::startTrans();
            try{

                $orderEdit['pay_status']    = 1;
                $orderEdit['pay_type']      = 1;
                $orderEdit['pay_time']      = time();

                ProductOrderM::where('id',$order)->update($orderEdit);


                Db::commit();
                echo "success";
            }catch (\Exception $e){

                Db::rollback();
                echo "fail";
            }

        }else{
            echo "fail";
        }
    }


    /**
     *   自助结账 回调
     **/
    public function self_wx_notify_url(){
        // 获取xml
        $xml        =   file_get_contents('php://input', 'r');
        // 转成php数组
        $arr        =   $this->toArray($xml);
        //商户订单号
        $order_sn   =   $arr['out_trade_no'];
        //支付宝交易号
        $pay_sn      =   $arr['transaction_id'];
        //订单金额
        $money      =   $arr['total_fee'];
        $order      =   Db::name('qt_parent_order')->where(['pay_no'=>$order_sn])->find();
        if($order['pay_status'] == 1)
        {
            echo "success";
            return;
        }
        if($order){

            Db::startTrans();
            try{

                //更新父级订单状态
                $orderEdit['pay_status'] = 1;
                $orderEdit['pay_type']   = 1;
                $orderEdit['pay_time']   = time();
                Db::name('qt_parent_order')->where('id',$order)->update($orderEdit);

                //更新项目订单状态
                if(!empty($order['order_id']))
                {

                    OrderM::where('id',$order['order_id'])->update([
                        'status'=>4,
                        'pay_type'      =>  1,
                        'pay_status'    =>  2,
                        'pay_time'      =>  time(),
                    ]);
                }

                //更新商品订单状态
                if(!empty($order['product_ids']))
                {

                    ProductOrderM::where('id',$order['product_ids'])->update([
                        'order_status'  =>  1,
                        'pay_type'      =>  1,
                        'pay_status'    =>  1,
                        'pay_time'      =>  time(),
                    ]);
                }




                Db::commit();
                echo "success";
            }catch (\Exception $e){

                Db::rollback();
                echo "fail";
            }

        }else{
            echo "fail";
        }
    }



    /**
     * 将xml转为array
     * @param  string $xml xml字符串
     * @return array       转换得到的数组
     */
    public function toArray($xml){
        //禁止引用外部xml实体
        libxml_disable_entity_loader(true);
        $result= json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $result;
    }
}
